﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using FileDistribution.appcode;

namespace FileDistribution
{
    public partial class LoadingMsg : Form
    {
        List<string> tags;
        string respath;
        StringBuilder error = new StringBuilder();
        MainForm mf;
        public LoadingMsg(List<string> _tags,string _respsth,MainForm _mf)
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            tags = _tags;
            respath = _respsth;
            mf = _mf;
            Thread thread = new Thread(() => Start());
            thread.Start();
           
        }

        private void Start()
        {
            foreach (var item in tags)
            {
                Logs.Line();
                Logs.Write("开始分发路径：" + item);
                FindFile(item, respath);
            }
            if (error.Length>0)
            {
                ShowError se = new ShowError(error);
                se.ShowDialog(mf);
            }
            else
            {
                MessageBox.Show("分发完毕！");
            }
            this.Close();
        }

        /// <summary>
        /// 查找文件
        /// </summary>
        /// <param name="tagPath">目标路径</param>
        /// <param name="loadPath">本地路径</param>
        private void FindFile(string tagPath, string loadPath)
        {

            DirectoryInfo dir = new DirectoryInfo(loadPath);
            foreach (var file in dir.GetFileSystemInfos())
            {
                if (file is FileInfo)
                {
                    var filename = file.Name;
                    var fullname = file.FullName;
                    string loadfile = tagPath + "/" + filename;
                    
                    labshow.Text = "正在分发：" + tagPath;
                    labfilename.Text = filename;

                    try
                    {
                        if (File.Exists(loadfile))
                        {
                            File.SetAttributes(loadfile, FileAttributes.Normal);
                        }
                        File.Copy(fullname, loadfile, true);
                    }
                    catch (Exception e)
                    {
                        error.AppendLine(loadfile);
                        error.AppendLine("分发错误:");
                        error.AppendLine(e.Message);
                        error.AppendLine("------------------------------------------------------");
                        Logs.Write("分发替换文件(" + filename + ")时出现错误：" + e.Message);
                        Logs.Line("详细错误信息");
                        Logs.Write(e.ToString());
                        Logs.Line("end");
                    }
                }
                else if (file is DirectoryInfo)
                {
                    DirectoryInfo d = file as DirectoryInfo;
                    string forders = tagPath + "/" + file.Name;
                    if (!Directory.Exists(forders))
                    {
                        Directory.CreateDirectory(forders);
                    }
                    FindFile(forders,file.FullName);
                }
            }

        }
    }
}
